//https://blog.csdn.net/zl1zl2zl3/article/details/81357146
import {PureComponent } from 'react';
import {
	Row,
	Col,
	Form,
	Input,
	Select,
	Modal,
	TreeSelect,
	DatePicker
} from 'antd';

import SelectDeptTree from '@/components/SelectDeptTree';
import SelectOrganTree from '@/components/SelectOrganTree';

const FormItem = Form.Item;
const { Option } = Select;


// 修改表单
@Form.create()
class RepaireForm extends PureComponent {
	static defaultProps = {
		handleUpdate: () => {},
		handleUpdateModalVisible: () => {},
		values: {},
	};

	constructor(props) {
		super(props);
		this.state = {
			formVals: {
				user_id: props.values.user_id,
				user_name: props.values.user_name,
				user_name_cn: props.values.user_name_cn,
				password: props.values.password,
				email: props.values.email,
				phone: props.values.phone,
				enabled: props.values.enabled,
				auth_ids: props.values.auth_ids,
				dept_id: props.values.dept_id,
				dept: props.values.dept,
				post_id: props.values.post_id,
				post_name: props.values.post_name
			},
			getDeptTreeBySelectTree: "",
			getOrganTreeBySelectTree: "",
		};

	}
	
	handleConfirmBlur = e => {
	    const { value } = e.target;
	    this.setState({ confirmDirty: this.state.confirmDirty || !!value });
	};

	compareToFirstPassword = (rule, value, callback) => {
	    const { form } = this.props;
	    if (value && value !== form.getFieldValue('password')) {
	      callback('两次输入的密码不一致!');
	    } else {
	      callback();
	    }
	};

	validateToNextPassword = (rule, value, callback) => {
	    const { form } = this.props;
	    if (value && this.state.confirmDirty) {
	      form.validateFields(['confirm'], { force: true });
	    }
	    callback();
	}

	selectedDeptTreeValue = (deptRecord) => {
		this.setState({
			getDeptTreeBySelectTree: deptRecord
		})
	}

	selectedOrganTreeValue = (orgRecord) => {
		this.setState({
			getOrganTreeBySelectTree: orgRecord
		})
	}

	handleLocalRepaire = () => {
		const { form,handleUpdate } = this.props;
		const { getDeptTreeBySelectTree } = this.state;
		const { formVals: oldValue } = this.state;
		form.validateFields((err, fieldsValue) => {
			if(err) return;
			const formVals = { ...oldValue,
				...fieldsValue
			};
			this.setState({
				formVals,
			});
			if(getDeptTreeBySelectTree) {
				formVals.dept_id = getDeptTreeBySelectTree.triggerNode.props.dataRef.dept_id;
				formVals.dept = getDeptTreeBySelectTree.triggerNode.props.dataRef.title;
			}
			
			if(formVals.post) {
				formVals.post_id = formVals.post.key;
				formVals.post_name = formVals.post.label;
			}
			
			handleUpdate(formVals);

		});
	};

	renderContent = (formVals, parentDeptTreeMethod, parentOrganTreeMethod) => {
		const { form,authForUser,postForUser } = this.props;
				//单位隶属关系
	const companyAffiliation = [
		  {
		    title: '中央',
		    value: '0',
		    key: '0',
		    children: [
		      {
		        title: '中央一级',
		        value: '0-1',
		        key: '0-1',
		      },
		      {
		        title: '中央设在各省(区、市)',
		        value: '0-2',
		        key: '0-2',
		      },
		      {
		        title: '中央设在副省级市',
		        value: '0-3',
		        key: '0-3',
		      },
		      {
		        title: '中央设在市(地、洲、盟)',
		        value: '0-5',
		        key: '0-5',
		      },
		      {
		        title: '中央设在市(地、洲、盟)',
		        value: '0-6',
		        key: '0-6',
		      },
		      {
		        title: '中央设在市(地、洲、盟)',
		        value: '0-7',
		        key: '0-7',
		      },
		    ],
		  },
		  {
		    title: '省',
		    value: '1',
		    key: '1',
		    children: [
		      {
		        title: '省(区、市)一级',
		        value: '1-1',
		        key: '1-1',
		      },
		      {
		        title: '省(区、市)设在副省级市',
		        value: '1-2',
		        key: '1-2',
		      },
		      {
		        title: '省(区、市)设在市(地、洲、盟)省级市',
		        value: '1-3',
		        key: '1-3',
		      },
		      {
		        title: '省(区、市)设直辖市辖区',
		        value: '1-4',
		        key: '1-4',
		      },
		      {
		        title: '省(区、市)设副省级市辖区',
		        value: '1-5',
		        key: '1-5',
		      },
		    ],
		  },
		  {
		    title: '副省级市',
		    value: '2',
		    key: '2',
		    children: [
		      {
		        title: '副省级市',
		        value: '2-1',
		        key: '2-1',
		      },
		    ],
		  },
		  {
		    title: '市(地、洲、盟)',
		    value: '3',
		    key: '3',
		    children: [
		      {
		        title: '省辖市',
		        value: '3-1',
		        key: '3-1',
		      },
		      {
		        title: '省辖市',
		        value: '3-1',
		        key: '3-1',
		      },
		      {
		        title: '地区',
		        value: '3-2',
		        key: '3-2',
		      },
		      {
		        title: '自治州',
		        value: '3-3',
		        key: '3-3',
		      },
		      {
		        title: '盟',
		        value: '3-4',
		        key: '3-4',
		      },
		    ],
		  },
		  {
		    title: '直辖区辖区',
		    value: '4',
		    key: '4',
		  },
		  {
		    title: '县(市、区、旗)',
		    value: '5',
		    key: '5',
		    children: [
		      {
		        title: '县级市',
		        value: '5-1',
		        key: '5-1',
		      },
		      {
		        title: '县',
		        value: '5-2',
		        key: '5-2',
		      },
		      {
		        title: '自治县',
		        value: '5-3',
		        key: '5-3',
		      },
		      {
		        title: '旗',
		        value: '5-4',
		        key: '5-4',
		      },
		      {
		        title: '自治旗',
		        value: '5-5',
		        key: '5-5',
		      },
		    ],
		  },
		  {
		    title: '乡(镇)、街道',
		    value: '6',
		    key: '6',
		    children: [
		      {
		        title: '镇',
		        value: '6-1',
		        key: '6-1',
		      },
		      {
		        title: '乡',
		        value: '6-2',
		        key: '6-2',
		      },
		      {
		        title: '街道',
		        value: '6-3',
		        key: '6-3',
		      },
		    ],
		  },

	];
	//单位性质类别
	const companyPropertyType = [
		  {
		    title: '公安行政机关',
		    value: '0',
		    key: '0',
		    children: [
		      {
		        title: '一级公安机关',
		        value: '0-1',
		        key: '0-1',
		      },
		      {
		        title: '内设机构',
		        value: '0-2',
		        key: '0-2',
		      },
		      {
		        title: '直属机关',
		        value: '0-3',
		        key: '0-3',
		      },
		      {
		        title: '派出机构',
		        value: '0-4',
		        key: '0-4',
		      },
		      {
		        title: '派出所',
		        value: '0-5',
		        key: '0-5',
		      },
		    ],
		  },
		  {
		    title: '事业单位',
		    value: '1',
		    key: '1',
		    children: [
		      {
		        title: '参照公务员管理的事业单位',
		        value: '1-1',
		        key: '1-1',
		      },
		      {
		        title: '其他事业单位',
		        value: '1-2',
		        key: '1-2',
		      },
		    ],
		  },
		  {
		    title: '其他',
		    value: '2',
		    key: '2',
		  },
		  {
		    title: '机构分组',
		    value: '3',
		    key: '3',
		  },
	];
	//单位级别
	const companyLevel = [
		  {
		    title: '机关',
		    value: '0',
		    key: '0',
		    children: [
		      {
		        title: '中央、国家级(正)',
		        value: '0-1',
		        key: '0-1',
		      },
		      {
		        title: '中央、国家级(副)',
		        value: '0-2',
		        key: '0-2',
		      },
		      {
		        title: '省、部级',
		        value: '0-3',
		        key: '0-3',
		      },
		      {
		        title: '副省、副部级',
		        value: '0-4',
		        key: '0-4',
		      },
		      {
		        title: '地、厅、司、局级',
		        value: '0-5',
		        key: '0-5',
		      },
		    ],
		  },
		  {
		    title: '解放军和公安现役部队',
		    value: '1',
		    key: '1',
		    children: [
		      {
		        title: '中央军委级',
		        value: '1-1',
		        key: '1-1',
		      },
		      {
		        title: '解放军总部级',
		        value: '1-2',
		        key: '1-2',
		      },
		      {
		        title: '大军区正级',
		        value: '1-3',
		        key: '1-3',
		      },
		      {
		        title: '大军区副级',
		        value: '1-4',
		        key: '1-4',
		      },
		      {
		        title: '正兵团级',
		        value: '1-5',
		        key: '1-5',
		      },
		      {
		        title: '副兵团级',
		        value: '1-6',
		        key: '1-6',
		      },
		    ],
		  },
		  {
		    title: '国际组织、机构',
		    value: '2',
		    key: '2',
		  },
		  {
		    title: '外国组织、机构',
		    value: '3',
		    key: '3',
		  },
		  {
		    title: '无级别',
		    value: '4',
		    key: '4',
		  },
		  {
		    title: '其他',
		    value: '5',
		    key: '5',
		  },
	];
	//单位所在政区
	const companyAddress = [
		  {
		    title: '北京市',
		    value: '0',
		    key: '0',
		    children: [
		      {
		        title: '北京市辖区',
		        value: '0-1',
		        key: '0-1',
		        children: [
			      {
			        title: '北京市东城区',
			        value: '0-1-1',
			        key: '0-1-1',
			      },
			      {
			        title: '北京市西城区',
			        value: '0-1-2',
			        key: '0-1-2',
			      },
			      {
			        title: '北京市朝阳区',
			        value: '0-1-3',
			        key: '0-1-3',
			      },
			      {
			        title: '北京市海淀区',
			        value: '0-1-4',
			        key: '0-1-4',
			      },
			      {
			        title: '北京市丰台区',
			        value: '0-1-5',
			        key: '0-1-5',
			      },
			    ],
			  },
			],
		  },
		  {
		    title: '天津市',
		    value: '1',
		    key: '1',
		    children: [
		      {
		        title: '天津市辖区',
		        value: '1-1',
		        key: '1-1',
		        children: [
			      {
			        title: '天津和平区',
			        value: '1-1-1',
			        key: '1-1-1',
			      },
			      {
			        title: '天津河东区',
			        value: '1-1-2',
			        key: '1-1-2',
			      },
			      {
			        title: '天津河西区',
			        value: '1-1-3',
			        key: '1-1-3',
			      },
			      {
			        title: '天津南开区',
			        value: '1-1-4',
			        key: '1-1-4',
			      },
			      {
			        title: '天津河北区',
			        value: '1-1-5',
			        key: '1-1-5',
			      },
			    ],
			  },
			],
		  },
		  {
		    title: '河北省',
		    value: '2',
		    key: '2',
		    children: [
		      {
		        title: '河北石家庄',
		        value: '2-1',
		        key: '2-1',
			  },
		      {
		        title: '河北唐山市',
		        value: '2-2',
		        key: '2-2',
			  },
		      {
		        title: '河北秦皇岛市',
		        value: '2-3',
		        key: '2-3',
			  },
		      {
		        title: '河北邯郸市',
		        value: '2-4',
		        key: '2-4',
			  },
		      {
		        title: '河北邢台市',
		        value: '2-5',
		        key: '2-5',
			  },
			],
		  },
		  {
		    title: '山西省',
		    value: '3',
		    key: '3',
		    children: [
		      {
		        title: '山西太原市',
		        value: '3-1',
		        key: '3-1',
			  },
		      {
		        title: '山西大同市',
		        value: '3-2',
		        key: '3-2',
			  },
		      {
		        title: '山西阳泉市',
		        value: '3-3',
		        key: '3-3',
			  },
		      {
		        title: '山西长治市',
		        value: '3-4',
		        key: '3-4',
			  },
		      {
		        title: '山西晋城市',
		        value: '3-5',
		        key: '3-5',
			  },
			],
		  },
		  {
		    title: '内蒙古自治区',
		    value: '4',
		    key: '4',
		    children: [
		      {
		        title: '内蒙古呼和浩特市',
		        value: '4-1',
		        key: '4-1',
			  },
		      {
		        title: '内蒙古包头市',
		        value: '4-2',
		        key: '4-2',
			  },
		      {
		        title: '内蒙古乌海市',
		        value: '4-3',
		        key: '4-3',
			  },
		      {
		        title: '内蒙古赤峰市',
		        value: '4-4',
		        key: '4-4',
			  },
		      {
		        title: '内蒙古通辽市',
		        value: '4-5',
		        key: '4-5',
			  },
			],
		  },

	];
	//单位所属部门与警种
	const companyPolice = [
		  {
		    title: '公安机关与公安分局[A0000]',
		    value: '0',
		    key: '0',
		    children: [
		      {
		        title: '公安部[A0100]',
		        value: '0-1',
		        key: '0-1',
		      },
		      {
		        title: '公安厅[A0200]',
		        value: '0-2',
		        key: '0-2',
		      },
		      {
		        title: '直辖市公安局[A0300]',
		        value: '0-3',
		        key: '0-3',
		      },
		      {
		        title: '副省级市公安局[A0400]',
		        value: '0-4',
		        key: '0-4',
		      },
		      {
		        title: '地级市公安局[A0500]',
		        value: '0-5',
		        key: '0-5',
		      },
		    ],
		  },
		  {
		    title: '警种和部门[B0000]',
		    value: '1',
		    key: '1',
		    children: [
		      {
		        title: '移民管理[B0100]',
		        value: '1-1',
		        key: '1-1',
		        children: [
			      {
			        title: '边境管理[B0101]',
			        value: '1-1-1',
			        key: '1-1-1',
			      },
			      {
			        title: '边防检查[B0102]',
			        value: '1-1-2',
			        key: '1-1-2',
			      },
			      {
			        title: '遣返管理[B0103]',
			        value: '1-1-3',
			        key: '1-1-3',
			      },
			      {
			        title: '其他[B0199]',
			        value: '1-1-4',
			        key: '1-1-4',
			      },
			    ],
		      },
		      {
		        title: '办公室[B0200]',
		        value: '1-2',
		        key: '1-2',
		        children: [
			      {
			        title: '机要文电[B0201]',
			        value: '1-2-1',
			        key: '1-2-1',
			      },
			      {
			        title: '督办[B0202]',
			        value: '1-2-2',
			        key: '1-2-2',
			      },
			      {
			        title: '保密[B0203]',
			        value: '1-2-3',
			        key: '1-2-3',
			      },
			      {
			        title: '其他[B0299]',
			        value: '1-2-4',
			        key: '1-2-4',
			      },
			    ],
		      },
		      {
		        title: '情报指挥[B0300]',
		        value: '1-3',
		        key: '1-3',
		        children: [
			      {
			        title: '情报信息[B0301]',
			        value: '1-3-1',
			        key: '1-3-1',
			      },
			      {
			        title: '指挥中心[B0302]',
			        value: '1-3-2',
			        key: '1-3-2',
			      },
			    ],
		      },
		      {
		        title: '研究室[B0400]',
		        value: '1-4',
		        key: '1-4',
		      },
		      {
		        title: '督察审计[B0500]',
		        value: '1-5',
		        key: '1-5',
		        children: [
			      {
			        title: '督察[B0501]',
			        value: '1-5-1',
			        key: '1-5-1',
			      },
			      {
			        title: '审计[B0502]',
			        value: '1-5-2',
			        key: '1-5-2',
			      },
			      {
			        title: '信访[B0503]',
			        value: '1-5-3',
			        key: '1-5-3',
			      },
			      {
			        title: '其他[B0599]',
			        value: '1-5-4',
			        key: '1-5-4',
			      },
			    ],
		      },
		    ],
		  },
	];
	//类型
	const companyType = [
		  {
		    title: '单位[UN]',
		    value: '0',
		    key: '0',
		  },
		  {
		    title: '虚单位[UO]',
		    value: '',
		    key: '1',
		  },
	];
	
return(
<Form  layout="vertical">

	<Row gutter={{ md: 8, lg: 24, xl: 48 }}>
    	<Col  md={12} sm={24}>
	        <FormItem  label="上级单位编码">
	            {form.getFieldDecorator('equal_company_code', {
	            rules: [{ required: false, message: '请输入上级单位编码！'}],
	            initialValue: "100000000000",
	            })(<Input placeholder="请输入" />)}
	        </FormItem>
        </Col>
        <Col  md={12} sm={24}>
	        <FormItem  label="上级单位名称">
	            {form.getFieldDecorator('equal_company_name', {
	            rules: [{ required: false, message: '请输入上级单位名称！'}],
	            initialValue: "全国公安机关",
	            })(<Input placeholder="请输入" />)}
	        </FormItem>
        </Col>
     
    </Row>
    <Row gutter={{ md: 8, lg: 24, xl: 48 }}>

        <Col  md={12} sm={24}>
	        <FormItem  label="单位编码">
	            {form.getFieldDecorator('company_code', {
	            rules: [{ required: true, message: '请输入单位编码！'}],
	            })(<Input placeholder="请输入" />)}
	        </FormItem>
        </Col>
        <Col  md={12} sm={24}>
	        <FormItem  label="机构树显示名称">
	            {form.getFieldDecorator('companytree_name', {
	            rules: [{ required: true, message: '请输入机构树显示名称！'}],
	            })(<Input placeholder="请输入" />)}
	        </FormItem>
        </Col>

    </Row>
    <Row gutter={{ md: 8, lg: 24, xl: 48 }}>

        <Col  md={12} sm={24}>
	        <FormItem  label="单位全称">
	            {form.getFieldDecorator('company_fullname', {
	            rules: [{ required: true, message: '请输入单位全称！'}],
	            })(<Input placeholder="请输入" />)}
	        </FormItem>
        </Col>
       	<Col  md={12} sm={24}>
	        <FormItem  label="成立时间">
	            {form.getFieldDecorator('create_time', {
	            	rules: [{ required: true, message: '请选择成立时间！'}],
	            })
	            (<DatePicker format="YYYY-MM-DD" style={{ width: '100%' }} placeholder="请选择"/>)}
	        </FormItem>
        </Col>

    </Row>
    
        
    <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
    
        <Col  md={12} sm={24}>
	        <FormItem  label="单位隶属关系">
	            {form.getFieldDecorator('company_affiliation', {
	            rules: [{ required: true, message: '请选择单位隶属关系！'}],
	            })(    
			      <TreeSelect
			      	showSearch
			        style={{ width: '100%' }}
			        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
			        treeData={companyAffiliation}
			        placeholder="请选择"
			        filterTreeNode={(input, treeNode) => 
			        	treeNode.props.title.toLowerCase().indexOf(input.toLowerCase()) >= 0
			        }
			      />
			    )}
	        </FormItem>
        </Col>
        <Col  md={12} sm={24}>
	        <FormItem  label="单位性质类别">
	            {form.getFieldDecorator('company_propertytype', {
	            rules: [{ required: true, message: '请选择单位性质类别！'}],
	            })(    
			      <TreeSelect
			      	showSearch
			        style={{ width: '100%' }}
			        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
			        treeData={companyPropertyType}
			        placeholder="请选择"
			        filterTreeNode={(input, treeNode) => 
			        	treeNode.props.title.toLowerCase().indexOf(input.toLowerCase()) >= 0
			        }
			      />
			    )}
	        </FormItem>
        </Col>
     
    </Row>
    <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
    
        <Col  md={12} sm={24}>
	        <FormItem  label="单位级别">
	            {form.getFieldDecorator('company_level', {
	            rules: [{ required: true, message: '请选择单位级别！'}],
	            })(    
			      <TreeSelect
			      	showSearch
			        style={{ width: '100%' }}
			        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
			        treeData={companyLevel}
			        placeholder="请选择"
			        filterTreeNode={(input, treeNode) => 
			        	treeNode.props.title.toLowerCase().indexOf(input.toLowerCase()) >= 0
			        }
			      />
			    )}
	        </FormItem>
        </Col>
        <Col  md={12} sm={24}>
	        <FormItem  label="单位所属部门与警种">
	            {form.getFieldDecorator('company_police', {
	            rules: [{ required: false, message: '请选择单位所属部门与警种！'}],
	            })(    
			      <TreeSelect
			      	showSearch
			        style={{ width: '100%' }}
			        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
			        treeData={companyPolice}
			        placeholder="请选择"
			        filterTreeNode={(input, treeNode) => 
			        	treeNode.props.title.toLowerCase().indexOf(input.toLowerCase()) >= 0
			        }
			      />
			    )}
	        </FormItem>
        </Col>
     
    </Row>
    <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
    
        <Col  md={12} sm={24}>
	        <FormItem  label="报组织部单位编码">
	            {form.getFieldDecorator('company_organcode', {
	            rules: [{ required: false, message: '请输入报组织部单位编码！'}],
	            })(<Input placeholder="请输入" />)}
	        </FormItem>
        </Col>
        <Col  md={12} sm={24}>
	        <FormItem  label="类型">
	            {form.getFieldDecorator('company_type', {
	            rules: [{ required: true, message: '请选择类型！'}],
	            })(    
			      <TreeSelect
			      	showSearch
			        style={{ width: '100%' }}
			        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
			        treeData={companyType}
			        placeholder="请选择"
			        filterTreeNode={(input, treeNode) => 
			        	treeNode.props.title.toLowerCase().indexOf(input.toLowerCase()) >= 0
			        }
			      />
			    )}
	        </FormItem>
        </Col>
     
    </Row>

</Form>
);

};

	render() {
		const {
			repaireModalVisible,
			handleRepaireModalVisible,
			values,
			dispatch,
			requestModelName,
		} = this.props;
		const {
			formVals
		} = this.state;

		return(
			<Modal
    width={800}
    centered
    bodyStyle={{ padding: '32px 40px 48px' }}
    destroyOnClose
    title="维护"
    visible={repaireModalVisible}
    onOk={() => this.handleLocalRepaire()}
    onCancel={() => handleRepaireModalVisible(false, values)}
    afterClose={() => handleRepaireModalVisible()}
>

    {this.renderContent(formVals)}
</Modal>
		);
	}

}

export default RepaireForm;